<?php
require APP_PATH.'Admin/Model/CategoryModel.class.php';
class IndexController extends Controller {
public function __init(){
	// 分配栏目
	$catDb=new CategoryModel;
	$category=$catDb->getAll();
	foreach ($category as $id => $cat) {
		if(isset($_GET['cid']) &&$_GET['cid']==$cat['cid']){
			$category[$id]['active']="class='active'";

		}else{
			$category[$id]['active']="";
		}
		
	}
	$this->assign('category',$category);
	// 获得最新的BLOG
	$db=new Model('blog');
	$sql='select * from blog order by bid desc limit 10';

	$this->assign('NewBlog',$db->query($sql));
	// 热门hot
	$sql='select * from blog order by click desc limit 10';
	$this->assign('HotBlog',$db->query($sql));
}

 public function index(){
   // 如果有cid参数，就读取cid的文章
 	if(isset($_GET['cid'])){
 		$where='where cid='.$_GET['cid'];
 	}else{
 		$where='';
 	}
 	$db=new Model('blog');
    $count = $db->query("select count(*) as c from blog $where");
    $page= new page($count[0]['c'],2);
    // 查找数据
    $sql="select * from blog order by bid desc limit ".$page->limit();
    $data=$db->query($sql);
    $this->assign('data',$data);
    $this->assign('page',$page->show());
    $this->display('index.html');
		
 }
// 博客详情
 public function blog(){
 	// 获得参数
 	$bid=$_GET['cid'];

 	$db=new Model('blog');
 	$sql="select * from blog as b join category as c on b.cid=c.cid where bid=$bid";
 	$filed=$db->query($sql);
 	$this->assign('filed',$filed);
 	$this->display('blog.html');
     
 }

// 获得点击数
 public function getClick(){
  $bid=$_GET['bid'];
  // 更新点击数
  $db=new Model('blog');
  $sql="update blog set click=click+1 where bid=$bid";
  $db->exe($sql);
  $filed=$db->where("bid=$bid")->find();
  echo "document.write({$filed['click']})";
 }
}



?>